package cn.signalpha.modules.shop.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.excel.util.ExcelUtil;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.DateUtil;
import cn.signalpha.core.tool.utils.StringPool;
import cn.signalpha.modules.shop.dto.OrderRefundDTO;
import cn.signalpha.modules.shop.entity.OrderRefund;
import cn.signalpha.modules.shop.excel.OrderRefundExcel;
import cn.signalpha.modules.shop.service.OrderRefundService;
import cn.signalpha.modules.shop.vo.OrderRefundVO;
import cn.signalpha.modules.shop.wrapper.OrderRefundWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/admin/order/refund")
@Api(value = "订单退换货", tags = "订单退换货")
public class OrderRefundController extends BaseController {

	private final OrderRefundService orderRefundService;

    @GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="sellerId", dataType = "string", value = "商户id"),
		@ApiImplicitParam(name="userId", dataType = "string", value = "用户id"),
		@ApiImplicitParam(name="orderNo", dataType = "string", value = "订单号"),
		@ApiImplicitParam(name="orderType", dataType = "string", value = "订单类型"),
		@ApiImplicitParam(name="refundNo", dataType = "string", value = "退款单号"),
		@ApiImplicitParam(name="refundType", dataType = "string", value = "售后类型"),
		@ApiImplicitParam(name="refundBizNo", dataType = "string", value = "第三方退款单号"),
		@ApiImplicitParam(name="refundStatus", dataType = "string", value = "售后状态"),
		@ApiImplicitParam(name="refundMoneyStatus", dataType = "string", value = "退款状态"),
		@ApiImplicitParam(name="logisticsNo", dataType = "string", value = "物流单号"),
		@ApiImplicitParam(name="createTimeBegin", dataType = "string", value = "开始时间"),
		@ApiImplicitParam(name="createTimeEnd", dataType = "string", value = "结束时间"),
	})
	@ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<OrderRefundVO>> list(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		QueryWrapper<OrderRefund> wrapper = orderRefundService.selectOrderRefundWrapper(params);
		IPage<OrderRefund> page = orderRefundService.page(Condition.getPage(query), wrapper);
		return R.data(OrderRefundWrapper.build().pageVO(page));
    }

	@GetMapping("/excel")
	@ApiOperation(value = "导出excel", notes = "")
	public void excel(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) {
		QueryWrapper<OrderRefund> wrapper = orderRefundService.selectOrderRefundWrapper(params);
		List<OrderRefundVO> orderRefundList = OrderRefundWrapper.build().listVO(orderRefundService.list(wrapper));
		List<OrderRefundExcel> orderExcelList = BeanUtil.copy(orderRefundList, OrderRefundExcel.class);
		ExcelUtil.export(response, DateUtil.time(), StringPool.EMPTY, orderExcelList, OrderRefundExcel.class);
	}

	@GetMapping("/info")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "详情", notes = "传入id")
	public R<OrderRefund> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(OrderRefundWrapper.build().entityVO(orderRefundService.getById(id)));
	}

	@PostMapping("/reject")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "拒绝", notes = "")
	public R reject(@Valid @RequestBody OrderRefundDTO.RejectDTO rejectDTO) {
		return R.status(orderRefundService.sellerReject(rejectDTO));
	}

	@PostMapping("/apply/agree")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "同意申请", notes = "")
	public R applyAgree(@Valid @RequestBody OrderRefundDTO.ApplyAgreeDTO applyAgreeDTO) {
		return R.status(orderRefundService.sellerAgreeApply(applyAgreeDTO));
	}

	@PostMapping("/shipping/agree")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "确认收货", notes = "")
	public R shippingAgree(@Valid @RequestBody OrderRefundDTO.IdDTO idDTO) {
		return R.status(orderRefundService.sellerAgreeShipping(idDTO.getId()));
	}

	@PostMapping("/pay")
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "重新打款", notes = "")
	public R pay(@Valid @RequestBody OrderRefundDTO.IdDTO idDTO) {
		return R.status(orderRefundService.sellerAgainPay(idDTO.getId()));
	}

}
